package com.tanhua.manage.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.MovementsVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserInfoApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MovementService {

    @Reference
    private PublishApi publishApi;

    @Reference
    private UserInfoApi userInfoApi;


    //动态分页业务
    public ResponseEntity<Object> findByPage(Integer page, Integer pagesize, String uid, Object state) throws InvocationTargetException, IllegalAccessException {
        PageResult pageResult = publishApi.queryByPage(page, pagesize, uid == null ? null : Long.valueOf(uid));
        List<Publish> items = (List<Publish>) pageResult.getItems();
        List<MovementsVo> voList = new ArrayList<>();

        if (items != null) {

            for (Publish publish : items) {
                // 3.2 封装vo对象
                MovementsVo vo = new MovementsVo();

                // 3.3 将publish的数据拷贝到vo中
                BeanUtils.copyProperties(vo, publish);

                // 3.4 封装用户详情信息
                UserInfo userInfo = userInfoApi.findById(publish.getUserId());

                // 3.5 将userInfo的数据拷贝到vo中
                if (userInfo != null) {
                    BeanUtils.copyProperties(vo, userInfo);
                    if (userInfo.getTags() != null) {
                        vo.setTags(userInfo.getTags().split(","));
                    }
                }
                // 3.6 处理动态中未能拷贝的字段
                vo.setId(publish.getId().toString());
                if (publish.getMedias() != null) {
                    vo.setImageContent(publish.getMedias().toArray(new String[]{}));
                }
                vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(publish.getCreated())));

                voList.add(vo);
            }
        }
        pageResult.setItems(voList);

        return ResponseEntity.ok(pageResult);
    }

    //动态详情
    public ResponseEntity<Object> findById(String id) throws InvocationTargetException, IllegalAccessException {
        Publish publish = publishApi.findById(id);
        // 4.1 创建要封装的vo对象
        MovementsVo vo = new MovementsVo();
        // 4.2 封装发布动态信息
        BeanUtils.copyProperties(vo, publish);
        // 4.3 封装用户信息
        Long userId = publish.getUserId();
        UserInfo userInfo = userInfoApi.findById(userId);
        if (userInfo != null) {
            BeanUtils.copyProperties(vo, userInfo);
        }
        // 4.4 其他参数
        vo.setId(publish.getId().toString());
        vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(publish.getCreated())));
        if (publish.getMedias() != null) {
            vo.setImageContent(publish.getMedias().toArray(new String[]{}));
            return ResponseEntity.ok(vo);
        }
        vo.setDistance("50米");
        return ResponseEntity.ok(vo);
    }

    //动态审核功能业务【实战】
    public ResponseEntity<Object> publishPass(List<String> payload) {
        if(payload != null)
            payload.forEach(s ->{
                publishApi.updateById(s,1 );
            });
        return ResponseEntity.ok("操作成功");
    }
}
